package com.leo.csdnspider.processor;

import com.leo.csdnspider.entity.BaseInfo;
import org.apache.log4j.Logger;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.text.SimpleDateFormat;

/**
 * @ClassName: CSDNPageProcessor
 * @Author: Leo
 * @Description: 博客基础信息
 * @Date: 8/29/2019 4:38 PM
 */
public class BaseInfoProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    private Logger logger = Logger.getLogger(BaseInfoProcessor.class);

    @Override
    public void process(Page page) {
        Html csdn = page.getHtml();

        //用户昵称
        String username = csdn.xpath("//*[@id=\"uid\"]/text()").get();

        //头像
        String photo = csdn.xpath("//*[@id=\"asideProfile\"]/div[1]/div[1]/a/img[1]/@src").get();

        //头像旁边的注册时长
        String age = csdn.xpath("//*[@id=\"asideProfile\"]/div[1]/div[1]/a/img[2]/@src").get();

        //原创文章数量
        Integer original = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[2]/dl[1]/dd/a/span/text()").get());

        //粉丝数量
        Integer fans = Integer.valueOf(csdn.xpath("//*[@id=\"fanBox\"]/@title").get());

        //被点赞数量
        Integer like = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[2]/dl[3]/@title").get());

        //评论数量
        Integer comment = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[2]/dl[4]/@title").get());

        //等级
        Integer grade = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[3]/dl[1]/dd/a/@title").get().split(",")[0].split("级")[0]);

        //访问量
        Integer visit = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[3]/dl[2]/dd/@title").get());

        //积分
        Integer jifen = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[3]/dl[3]/dd/@title").get());

        //排名
        Integer ranking = Integer.valueOf(csdn.xpath("//*[@id=\"asideProfile\"]/div[3]/dl[4]/@title").get());

        //userID
        String urlUsername = csdn.xpath("/html/head/link[1]/@href").get().substring(22);

        //爬取时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(System.currentTimeMillis());

        BaseInfo baseInfo = new BaseInfo(null, urlUsername, username, photo, age, original, fans, like, comment, grade, visit, jifen, ranking, date);
        page.putField("baseInfo", baseInfo);
        logger.info(" 基础信息 -----> " + baseInfo);
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider spider = Spider.create(new BaseInfoProcessor());
        spider.addUrl("https://blog.csdn.net/guolin_blog");
        spider.thread(5);
        spider.setExitWhenComplete(true);
        spider.start();
        spider.stop();
    }
}
